home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / post1.0 / ui / example-swing / AppletDemo.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  3.5 KB  |  107 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.applet.Applet;
  18. import java.awt.*;
  19. import java.awt.event.*;
  20. import com.sun.java.swing.AbstractButton;
  21. import com.sun.java.swing.JButton;
  22. import com.sun.java.swing.JPanel;
  23. import com.sun.java.swing.JFrame;
  24. import com.sun.java.swing.ImageGlyph;
  25.  
  26. /**
  27.  * This would have been a simple wrapper around ButtonDemo, if not
  28.  * for the fact that you apparently can't load images in Swing-based
  29.  * applets (due to security limitations in the implementation).
  30.  *
  31.  * So instead, this applet duplicates ButtonDemo, omitting the code
  32.  * that creates images in the buttons.
  33.  */
  34. public class AppletDemo extends Applet
  35.                         implements java.awt.event.ActionListener {
  36.  
  37. JButton b1, b2, b3;
  38.  
  39.     public void init() {
  40.     //ImageGlyph leftButtonGlyph = new ImageGlyph("right.gif");
  41.     //ImageGlyph middleButtonGlyph = new ImageGlyph("middle.gif");
  42.     //ImageGlyph rightButtonGlyph = new ImageGlyph("left.gif");
  43.  
  44.         //b1 = new JButton("Disable middle button", leftButtonGlyph);
  45.         b1 = new JButton("Disable middle button");
  46.     b1.setVerticalTextPosition(AbstractButton.CENTER);
  47.     b1.setHorizontalTextPosition(AbstractButton.LEFT);
  48.     b1.setKeyAccelerator('d');
  49.     b1.setActionCommand("disable");
  50.  
  51.         //b2 = new JButton("Middle button", middleButtonGlyph);
  52.         b2 = new JButton("Middle button");
  53.     b2.setVerticalTextPosition(AbstractButton.BOTTOM);
  54.     b2.setHorizontalTextPosition(AbstractButton.CENTER);
  55.     b2.setKeyAccelerator('m');
  56.  
  57.         //b3 = new JButton("Enable middle button", rightButtonGlyph);
  58.         b3 = new JButton("Enable middle button");
  59.     //Use the default text position of CENTER, RIGHT.
  60.     b3.setKeyAccelerator('e');
  61.     b3.setActionCommand("enable");
  62.         b3.setEnabled(false);
  63.  
  64.     //Listen for actions on buttons 1 and 3.
  65.     b1.addActionListener(this);
  66.     b3.addActionListener(this);
  67.  
  68.         //Add Components to the JPanel, using the default FlowLayout. 
  69.         add(b1);
  70.         add(b2);
  71.         add(b3);
  72.     }
  73.  
  74.     public void actionPerformed(java.awt.event.ActionEvent e) {
  75.         if (e.getActionCommand().equals("disable")) {
  76.             b2.setEnabled(false);
  77.             b1.setEnabled(false);
  78.             b3.setEnabled(true);
  79.         } else { 
  80.             b2.setEnabled(true);
  81.             b1.setEnabled(true);
  82.             b3.setEnabled(false);
  83.         }
  84.     }
  85.     
  86.     public static void main(String[] args) {
  87.     /*
  88.      * Create a window.  Use JFrame since this window will include 
  89.      * lightweight components.
  90.      */
  91.     JFrame frame = new JFrame("AppletDemo");
  92.  
  93.     WindowListener l = new WindowAdapter() {
  94.         public void windowClosing(WindowEvent e) {System.exit(0);}
  95.     };
  96.     frame.addWindowListener(l);
  97.  
  98.     AppletDemo applet = new AppletDemo();
  99.     applet.init();
  100.     frame.add("Center", applet);
  101.     frame.pack();
  102.     frame.show();
  103.     }
  104. }
  105.  
  106.  
  107.